//请求后台，获取json格式的商品信息
function onLoad(){
	$.ajax({
		type: 'GET',
		url: '/recommend',
		dataType: 'json',
		success: function(data){
			init_item_list(data);
		},
		fail: function(args){
			console.log('request failed');
		},
		error: function(args){
		    console.log('returned data incorrect');
		}
	});
	initNav();
}

//根据json中的商品信息向三个列表中添加商品标签
function init_item_list(data){
	var lists=[document.getElementById("uList_L"),document.getElementById("uList_M"),document.getElementById("uList_R")];
	for(i=0;i<data.length;i++){
		var img=data[i]["img"];
		var name=data[i]["name"];
		var price=data[i]["price"];
		var des=data[i]["des"];
		var gid=data[i]["gid"];
		
		var item=create_goods_item(img,name,price,des,gid);
		lists[i%3].appendChild(item);
	}
}

//创建一个商品标签元素
function create_goods_item(img_s,name_s,price_s,des_s,gid_s){
	var new_item=document.createElement("div");
	new_item.setAttribute("class","item");
	new_item.setAttribute("gid",gid_s);
	//img
	var img=document.createElement("img");
	img.setAttribute("class","content");
	img.setAttribute("src",img_s);
	//name
	var name=document.createElement("text");
	name.setAttribute("class","name");
	name.textContent=name_s;
	//yuan
	var yuan=document.createElement("text");
	yuan.setAttribute("class","yuan");
	yuan.textContent="￥";
	//price
	var price=document.createElement("text");
	price.setAttribute("class","price");
	price.textContent=price_s;
	//description
	var des=document.createElement("text");
	des.setAttribute("class","description");
	des.textContent=des_s;
	//append:
	new_item.appendChild(img);
	new_item.appendChild(document.createElement("br"));
	new_item.appendChild(name);
	new_item.appendChild(yuan);
	new_item.appendChild(price);
	new_item.appendChild(document.createElement("br"));
	new_item.appendChild(des);
	new_item.setAttribute("onclick","window.open('/item_"+gid_s+"')");
	return new_item;
}